﻿using System;

using System.Data;
using System.Data.SqlClient;

namespace IFD2
{
    public partial class OrderPassengers : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["ACL"].ToString().IndexOf("SA1") < 0)//BB4增删旅客名单是事件，不是权限！此处是生成订单权限
                {
                    Response.Redirect("unauthorized.aspx");
                }
            }
            DataTable myTab = new DataTable();
            SqlCommand cmd = new SqlCommand();
            DataRow row;
            butSubmitOrder.Attributes.Add("onclick", "javascript:return confirm('Are you sure?');");
            butCancel.Attributes.Add("onclick", "javascript:return confirm('Are you sure?');");
            btnDelete.Attributes.Add("onclick", "javascript:return confirm('Are you sure?');");
            btnUpdate.Attributes.Add("onclick", "javascript:return confirm('Are you sure?');");
            if (IsPostBack)
            {
                lblError.Text = "";
            }
            else
            {
                if (Request.QueryString["pnr"] != null)
                {
                    lblOrderNo.Text = Request.QueryString["pnr"].ToString();
                    Session["BK_ID"] = lblOrderNo.Text;
                    if (Request.QueryString["is_new"] != null)
                    {
                        if (Request.QueryString["is_new"].ToString() == "T")
                        {
                            //支付时的兑换率可能变化，以支付前为准。
                        }
                    }
                }
                else if (Session["BK_ID"] != null)
                {
                    lblOrderNo.Text = Session["BK_ID"].ToString();
                }

                if (lblOrderNo.Text != "")
                {
                    //DataTable myTab = new DataTable();
                    //SqlCommand cmd = new SqlCommand();
                    //DataRow row;
                    cmd.CommandText = "bll_bk_get_pax_count";
                    SqlParameter pLang = cmd.Parameters.AddWithValue("@bk_id", Convert.ToInt64(lblOrderNo.Text));
                    try
                    {
                        TinyDAL.Exec(ref cmd, ref myTab);
                        if (myTab.Rows.Count == 1)
                        {
                            row = myTab.Rows[0];
                            lblAdtCount.Text = row["adt_count"].ToString();
                            lblChdCount.Text = row["chd_count"].ToString();
                            lblInfCount.Text = row["inf_count"].ToString();
                            lbtnQuery_Click(sender, e);
                            //lblError.Text = "";
                            //if (row["names_ready"] == 1)
                            //{
                            //    butAdd.Visible = false;
                            //    butDelete.Visible = false;
                            //    chkConfirm.Visible = false;
                            //    butCommitOrder.Visible = false;
                            //}
                            //else
                            //{
                            //    butAdd.Visible = true;
                            //    butDelete.Visible = true;
                            //    chkConfirm.Visible = true;
                            //    butCommitOrder.Visible = true;
                            //}
                            //hidOrderNo.Value = txtPnrNo.Text;
                            ////FillData();
                        }
                        else
                        {
                            lblError.Text = "Order not found!";
                            lblAdtCount.Text = "0";
                            lblChdCount.Text = "0";
                            lblInfCount.Text = "0";
                        }

                    }
                    catch (Exception ex)
                    {
                        if (Application["DEBUG"].ToString() == "1")
                            lblError.Text = ex.Message;
                        else
                            lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
                        return;
                    }

                }
            }
        }

        protected void lbtnQuery_Click(object sender, EventArgs e)
        {
            GridView1.DataBind();
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("SA4") < 0)
            {
                lblError.Text = "Unauthorized!";
                return;
            }
            //离港批量签转需要SQL技巧：select * from tkt_root where (bk_id*1000+pax_sn) in ((90000903414*1000+1),(90000903451*1000+2))
            //所以pax_sn不能超过999！
            try
            {
                DateTime.Parse(txtExpiry.Text);
                if(txtInfantDob.Text!="")
                    DateTime.Parse(txtInfantDob.Text);
            }
            catch
            {
                lblError.Text = "Invalid date format!";
                return;
            }
            bool is_male;
            int ret_code;
            txtFamilyNameEn.Text = txtFamilyNameEn.Text.Trim();
            txtNameEn.Text = txtNameEn.Text.Trim();
            if (txtFamilyNameEn.Text == "" || txtNameEn.Text == "")
            {
                lblError.Text = "Please input passenger name(EN).";
                return;
            }
            if (radMale.Checked)
                is_male = true;
            else if (radFemale.Checked)
                is_male = false;
            else
            {
                lblError.Text = "Please input passenger gender.";
                return;
            }
            txtInfantDob.Text = txtInfantDob.Text.Trim();
            //txtInfName.Text = txtInfName.Text.Trim();

            if (lstAgeType.SelectedValue == "I" && txtInfantDob.Text == "")
            //if (chkHoldInfant.Checked && (txtInfantDob.Text == "" || txtInfName.Text == ""))
            {
                lblError.Text = "Infant's birthday is needed!";
                return;
            }
            txtIdNo.Text = txtIdNo.Text.Trim();
            txtExpiry.Text = txtExpiry.Text.Trim();
            if (txtIdNo.Text == "" || txtExpiry.Text == "")
            {
                lblError.Text = "ID No. and ID expiry are needed!";
                return;
            }
            if (chkHoldInfant.Checked && (lstAgeType.SelectedValue == "C" || lstAgeType.SelectedValue == "I"))
            {
                lblError.Text = "A child or infant passenger can not hold infant!";
                return;
            }
        
            try
            {
                SqlCommand cmd = new SqlCommand("bll_bk_add_pax");
                SqlParameter para = cmd.Parameters.AddWithValue("@bk_id", lblOrderNo.Text);
                para = cmd.Parameters.AddWithValue("@ffp_airline", lstFfpAirlines.SelectedValue);
                para = cmd.Parameters.AddWithValue("@ffp_id", txtFfpNo.Text);
                para = cmd.Parameters.AddWithValue("@pax_value", txtPaxValue.Text);
                para = cmd.Parameters.AddWithValue("@family_name_en", txtFamilyNameEn.Text);
                para = cmd.Parameters.AddWithValue("@name_en", txtNameEn.Text);
                para = cmd.Parameters.AddWithValue("@family_name", txtFamilyName.Text);
                para = cmd.Parameters.AddWithValue("@name", txtName.Text);
                para = cmd.Parameters.AddWithValue("@hold_inf", chkHoldInfant.Checked);
                para = cmd.Parameters.AddWithValue("@male", is_male);
                para = cmd.Parameters.AddWithValue("@age_type", lstAgeType.SelectedValue);
                para = cmd.Parameters.AddWithValue("@inf_dob", txtInfantDob.Text);
                para = cmd.Parameters.AddWithValue("@nationality", lstNations.SelectedValue);
                para = cmd.Parameters.AddWithValue("@id_type", lstIdTypes.SelectedValue);
                para = cmd.Parameters.AddWithValue("@id_no", txtIdNo.Text);
                para = cmd.Parameters.AddWithValue("@id_expired_date", txtExpiry.Text);
                para = cmd.Parameters.AddWithValue("@mobile_phone", txtPhone.Text);
                para = cmd.Parameters.AddWithValue("@email", txtEmail.Text);
                para = cmd.Parameters.AddWithValue("@vip", chkVip.Checked);
                para = cmd.Parameters.AddWithValue("@alone_chd", chkAloneChd.Checked);
                para = cmd.Parameters.AddWithValue("@move_assistant", lstMoveAssistant.SelectedValue);
                para = cmd.Parameters.AddWithValue("@meal", lstMeal.SelectedValue);
                para = cmd.Parameters.AddWithValue("@remark", txtRemark.Text);
                para = cmd.Parameters.AddWithValue("@need_invoice", chkNeedInvoice.Checked);
                para = cmd.Parameters.AddWithValue("@mail_address", txtMailAddress.Text);
                para = cmd.Parameters.AddWithValue("@post_code", txtPostCode.Text);
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                para = cmd.Parameters.AddWithValue("@office", Session["OFFICE"].ToString());
                para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());

                ret_code = TinyDAL.Exec(ref cmd);
                switch (ret_code)
                {
                    case 0:
                        lblError.Text = "Done.";
                        GridView1.DataBind();
                        break;
                    case 1:
                        lblError.Text = "1:Order not found!";
                        break;
                    case 2:
                        lblError.Text = "2:Updating pax S/N failed!";
                        break;
                    //case 3:
                    //    lblError.Text = "3:Name list is full!";
                    //    break;
                    case 4:
                        lblError.Text = "4:Illegal name list status!";
                        break;
                    case 5:
                        lblError.Text = "5:Pax in this age is full!";
                        break;
                    case 6:
                        lblError.Text = "6:Illegal pax count!";
                        break;
                    case 7:
                        lblError.Text = "7:Adding pax failed!";
                        break;
                    case 8:
                        lblError.Text = "A child or infant passenger can not hold infant!";
                        break;
                    case 9:
                        lblError.Text = "Illegal age code!";
                        break;
                    //case 10:
                    //    lblError.Text = "Infant passenger can not choise this meal!";
                    //    break;
                    case 11:
                        lblError.Text = "Non-child passenger can not check 'Alone Child'!";
                        break;
                    case 12:
                        lblError.Text = "Infant's DOB is needed!";
                        break;
                    case 13:
                        lblError.Text = "Order trip not found!";
                        break;
                    //case 14:
                    //    lblError.Text = "Duplicated booking!";//允许重复订座，提供报表控制
                    //    break;
                    case 15:
                        lblError.Text = "Too many pax_sn!";
                        break;
                    default:
                        lblError.Text = "Error:" + ret_code.ToString() + "!";
                        break;
                }

            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }
        }
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("SA4") < 0)
            {
                lblError.Text = "Unauthorized!";
                return;
            }

            int ret_code;
            string key;
            string[] strArray;
            if (Request.Form["KEY"] == null)
            {
                lblError.Text = "Please select the passenger.";
                return;
            }
            key = Request.Form["KEY"];
            strArray = key.Split('|');

            SqlCommand cmd = new SqlCommand();
            lblError.Text = "";
            try
            {
                cmd.CommandText = "bll_bk_delete_pax";
                SqlParameter para = cmd.Parameters.AddWithValue("@bk_id", strArray[0]);
                para = cmd.Parameters.AddWithValue("@pax_sn",strArray[1]);
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                para = cmd.Parameters.AddWithValue("@office", Session["OFFICE"].ToString());
                para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());

                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                {
                    lblError.Text = "Done.";
                    GridView1.DataBind();
                }
                else if (ret_code == 1)
                    lblError.Text = "Order not found,or can not change pax. in current status!";
                else if (ret_code == 2)
                    lblError.Text = "Can not change pax. in current status!";
                else
                    lblError.Text = "Unknow Error:" + ret_code.ToString();
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }

        }

        protected void butCancel_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("SA4") < 0)
            {
                lblError.Text = "Unauthorized!";
                return;
            }

            try
            {
                int ret_code;
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = "bll_bk_cancel";
                SqlParameter para = cmd.Parameters.AddWithValue("@bk_id", lblOrderNo.Text);
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                para = cmd.Parameters.AddWithValue("@user_airline", Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@office", Session["OFFICE"].ToString());
                para = cmd.Parameters.AddWithValue("@office_type", Session["OFFICE_TYPE"].ToString());
                para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());

                ret_code = TinyDAL.Exec(ref cmd);
                switch (ret_code)
                {
                    case 0:
                        lblError.Text = "Order Cancelled.";
                        break;
                    case 1:
                        lblError.Text = "Updating cabin failed!";
                        break;
                    case 2:
                        lblError.Text = "Updating class failed!";
                        break;
                    case 3:
                        lblError.Text = "Releasing blocked cabin failed!";
                        break;
                    case 4:
                        lblError.Text = "Releasing blocked class failed!";
                        break;
                    case 5:
                        lblError.Text = "Can not cancel order in current status, or order not found!";
                        break;
                    case 6:
                        lblError.Text = "Check authority!";
                        break;
                    case 81:
                        lblError.Text = "Cancelling order root failed!";
                        break;
                    case 90:
                        lblError.Text = "Cancelling order trips failed!";
                        break;
                    case 150:
                        lblError.Text = "Re-Calculate nested class avail failed!";
                        break;
                    default:
                        lblError.Text = "Error:" + ret_code.ToString();
                        break;
                }
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }
        }
        protected void butSubmitOrder_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("SA4") < 0)
            {
                lblError.Text = "Unauthorized!";
                return;
            }
            //if (!chkConfirm.Checked)//团体订单无需名单完整！
            //{
            //    //lblError.Text = "请确认名单已完整，在“旅客名单已完整”旁打勾，然后再按“提交”按钮。";
            //    lblError.Text = "Please confirm the name list is ready and check the checkbox!";
            //    return;
            //}
            try
            {
                int ret_code;
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = "bll_bk_submit";
                SqlParameter para = cmd.Parameters.AddWithValue("@bk_id", lblOrderNo.Text);
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                para = cmd.Parameters.AddWithValue("@office", Session["OFFICE"].ToString());
                para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());

                ret_code = TinyDAL.Exec(ref cmd);
                switch (ret_code)
                {
                    case 0:
                        //lblError.Text = "Order committed.";
                        //Response.Redirect("pay_order2.aspx?pnr=" + lblOrderNo.Text);
                        //转往订单可检查订单内容后再支付，而不是直接支付
                        Session["BK_ID"] = lblOrderNo.Text;
                        Response.Redirect("Order.aspx");//?pnr=" + lblOrderNo.Text);
                        break;
                    case 1:
                        lblError.Text = "Order not found!";
                        break;
                    case 2:
                        lblError.Text = "Commition failed!";
                        break;
                    case 3:
                        lblError.Text = "Order passengers not ready!";
                        break;
                    default:
                        lblError.Text = "Error: " + ret_code.ToString();
                        break;
                }
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }
        }
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("SA5") < 0)
            {
                lblError.Text = "Unauthorized!";
                return;
            }

            //旅客年龄类型不能修改！低年龄可以充高年龄？
            string key;
            string[] strArray;
            if (Request.Form["KEY"] == null)
            {
                lblError.Text = "Please select the passenger.";
                return;
            }
            key = Request.Form["KEY"];
            strArray = key.Split('|');

            bool is_male;
            int ret_code;
            txtFamilyName.Text = txtFamilyName.Text.Trim();
            txtName.Text = txtName.Text.Trim();
            if (txtFamilyName.Text == "" || txtName.Text == "")
            {
                lblError.Text = "Please input passenger name.";
                return;
            }
            if (radMale.Checked)
                is_male = true;
            else if (radFemale.Checked)
                is_male = false;
            else
            {
                lblError.Text = "Please input passenger gender.";
                return;
            }
            txtInfantDob.Text = txtInfantDob.Text.Trim();
            if (lstAgeType.SelectedValue == "I" && txtInfantDob.Text == "")
            {
                lblError.Text = "Infant passenger's DOB is needed!";
                return;
            }
            txtIdNo.Text = txtIdNo.Text.Trim();
            txtExpiry.Text = txtExpiry.Text.Trim();
            if (txtIdNo.Text == "" || txtExpiry.Text == "")
            {
                lblError.Text = "ID No. and ID expiry are needed!";
                return;
            }

            try
            {
                SqlCommand cmd = new SqlCommand("bll_bk_change_pax");
                SqlParameter para = cmd.Parameters.AddWithValue("@bk_id", strArray[0]);
                para = cmd.Parameters.AddWithValue("@pax_sn", strArray[1]);
                para = cmd.Parameters.AddWithValue("@ffp_airline", lstFfpAirlines.SelectedValue);
                para = cmd.Parameters.AddWithValue("@ffp_id", txtFfpNo.Text);
                para = cmd.Parameters.AddWithValue("@pax_value", txtPaxValue.Text);
                para = cmd.Parameters.AddWithValue("@family_name_en", txtFamilyNameEn.Text);
                para = cmd.Parameters.AddWithValue("@name_en", txtNameEn.Text);
                para = cmd.Parameters.AddWithValue("@family_name", txtFamilyName.Text);
                para = cmd.Parameters.AddWithValue("@name", txtName.Text);
                para = cmd.Parameters.AddWithValue("@hold_inf", chkHoldInfant.Checked);
                para = cmd.Parameters.AddWithValue("@male", is_male);
                //para = cmd.Parameters.AddWithValue("@age_type", lstAgeType.SelectedValue);
                //if (chkWithInf.Checked)
                //{
                //    para = cmd.Parameters.AddWithValue("@with_inf", true);
                //    para = cmd.Parameters.AddWithValue("@inf_name", txtInfName.Text);
                //    para = cmd.Parameters.AddWithValue("@inf_dob", txtInfantDob.Text);
                //}
                //else
                //{
                //    para = cmd.Parameters.AddWithValue("@with_inf", false);
                //    para = cmd.Parameters.AddWithValue("@inf_name", "");
                //    para = cmd.Parameters.AddWithValue("@inf_dob", "1900-1-1");
                //}
                para = cmd.Parameters.AddWithValue("@inf_dob", txtInfantDob.Text);
                para = cmd.Parameters.AddWithValue("@nationality", lstNations.SelectedValue);
                para = cmd.Parameters.AddWithValue("@id_type", lstIdTypes.SelectedValue);
                para = cmd.Parameters.AddWithValue("@id_no", txtIdNo.Text);
                para = cmd.Parameters.AddWithValue("@id_expired_date", txtExpiry.Text);
                para = cmd.Parameters.AddWithValue("@mobile_phone", txtPhone.Text);
                para = cmd.Parameters.AddWithValue("@email", txtEmail.Text);
                para = cmd.Parameters.AddWithValue("@vip", chkVip.Checked);
                para = cmd.Parameters.AddWithValue("@alone_chd", chkAloneChd.Checked);
                para = cmd.Parameters.AddWithValue("@move_assistant", lstMoveAssistant.SelectedValue);
                para = cmd.Parameters.AddWithValue("@meal", lstMeal.SelectedValue);
                para = cmd.Parameters.AddWithValue("@remark", txtRemark.Text);
                para = cmd.Parameters.AddWithValue("@need_invoice", chkNeedInvoice.Checked);
                para = cmd.Parameters.AddWithValue("@mail_address", txtMailAddress.Text);
                para = cmd.Parameters.AddWithValue("@post_code", txtPostCode.Text);
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                para = cmd.Parameters.AddWithValue("@office", Session["OFFICE"].ToString());
                para = cmd.Parameters.AddWithValue("@office_type", Session["OFFICE_TYPE"].ToString());
                para = cmd.Parameters.AddWithValue("@user_airline", Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());

                ret_code = TinyDAL.Exec(ref cmd);
                switch (ret_code)
                {
                    case 0:
                        lblError.Text = "Done.";
                        GridView1.DataBind();
                        break;
                    case 1:
                        lblError.Text = "1:Order not found!";
                        break;
                    case 3:
                        lblError.Text = "3:Changing pax failed!";
                        break;
                    case 4:
                        lblError.Text = "4:Check authorization!";
                        break;
                    case 5:
                        lblError.Text = "5:Can not change pax name in current status!";
                        break;
                    case 8:
                        lblError.Text = "A child or infant passenger can not travel with any infant!";
                        break;
                    case 9:
                        lblError.Text = "Passenger not found!";
                        break;
                    case 11:
                        lblError.Text = "Non-child passenger can not check 'Alone Child'!";
                        break;
                    case 13:
                        lblError.Text = "Order trip not found!";
                        break;
                    case 14:
                        lblError.Text = "Duplicated booking!";
                        break;
                    default:
                        lblError.Text = "Error:" + ret_code.ToString() + "!";
                        break;
                }

            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }
        }
        protected void lstAgeType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lstAgeType.SelectedValue == "A")
            {
                lblInfDob.Visible = false;
                txtInfantDob.Visible = false;
                chkHoldInfant.Visible = true;
                chkAloneChd.Checked = false;
                chkAloneChd.Visible = false;
            }
            else if (lstAgeType.SelectedValue == "I")
            {
                lblInfDob.Visible = true;
                txtInfantDob.Visible = true;
                chkHoldInfant.Checked = false;
                chkHoldInfant.Visible = false;
                chkAloneChd.Checked = false;
                chkAloneChd.Visible = false;
            }
            else
            {
                lblInfDob.Visible = false;
                txtInfantDob.Visible = false;
                chkHoldInfant.Checked = false;
                chkHoldInfant.Visible = false;
                chkAloneChd.Visible = true;
            }
        }
    }
}